﻿using _80X_CG_Frame.Interfaces;
using _80X_CG_Frame.Objects;
using _80X_CG_Frame.Utils;
using System;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using System.Collections.Generic;
using System.Runtime.CompilerServices;
using System.ComponentModel;
using System.IO;
using System.IO.Compression;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;

namespace _80X_CG_Frame.Workers
{
    /// <summary>
    /// 线程全局
    /// </summary>
    public static class GLOBAL
    {
        #region 全局变量声明
        /// <summary>
        /// 用户
        /// </summary>
        public static User user;
        /// <summary>
        /// 配置项
        /// </summary>
        public static ConfigUtil config;
        /// <summary>
        /// 视觉
        /// </summary>
        public static ICameras cameras;
        /// <summary>
        /// 激光器
        /// </summary>
        public static Dictionary<string, ILaserEngine> laser;
        /// <summary>
        /// SFC服务器日志上传接口
        /// </summary>
        public static ILogUpload logUpload;
        public static IPLC plc;
        //public static I扫码枪 扫码枪;
        public static Dictionary<string, I扫码枪> 扫码枪;
        /// <summary>
        /// 机器手
        /// </summary>
        public static Dictionary<string, IRobot> robot;
        /// <summary>
        /// SFC
        /// </summary>
        public static SFC sfc;
        /// <summary>
        /// 工位
        /// </summary>
        public static List<Station> station;
        /// <summary>
        /// 寄存器为PLC前急停按钮
        /// </summary>
        public static string 运行标志;

        private static JObject config_json;
        /// <summary>
        /// 寄存器为PLC总警告
        /// </summary>
        public static string 总警告;
        /// <summary>
        /// 刚下的料
        /// </summary>
        public static Tray 下料位状态;
        /// <summary>
        /// 统计条码结果
        /// </summary>
        public static Statistics 统计数据;

        #endregion

        static GLOBAL()
        {
            config = new ConfigUtil("./config.json");
            config.ReadConfig();

            config_json = (JObject)GLOBAL.config.configs["SFC_demo"];
            cameras = new Cameras();
            laser = new Dictionary<string, ILaserEngine>
            {
                { "激光机台1", new LaserEngine() },
                { "激光机台2", new LaserEngine() }
                //{ "激光机台3", new LaserEngine() }
            };
            logUpload = new LogUpload();
            var plc_type = GLOBAL.config.Get("PLC类型");
            if (plc_type == "三菱")
            {
                plc = new PLC();
            }
            else if (plc_type == "欧姆龙")
            {
                plc = new OMRON();
            }
            sfc = new SFC(config.Get("过站报告_param"), config.Get("上料验证_param"), config.Get("焊接参数_param"));
            扫码枪 = new Dictionary<string, I扫码枪>
            {
                {"扫码枪1",new 扫码枪() },
                {"扫码枪2",new 扫码枪()}
            };

            if (GLOBAL.config.Get("机器人类型") == "富士康")
            {
                robot = new Dictionary<string, IRobot>
                {
                    {"机器手1", new 富士康_Robot() },
                    {"机器手2", new 富士康_Robot() }
                };
            }
            else if (GLOBAL.config.Get("机器人类型") == "爱普生")
            {
                robot = new Dictionary<string, IRobot>
                {
                    {"机器手1", new 爱普生_Robot()},
                    {"机器手2", new 爱普生_Robot() }
                };
            }
            station = new List<Station>();
            for (int i = 0; i < 9; i++)
            {
                Station temp = new Station();
                if (i == 0)
                    temp.ID = "中转工位";
                else
                    temp.ID = "工位" + i;
                temp.Tray = new Tray("载具" + i);
                //temp.Tray.Product = new Product();

                station.Add(temp);
            }

            运行标志 = "0";
            下料位状态 = new Tray("下料位");
            统计数据 = new Statistics();
            user = new User();
        }


        #region 全局方法
        /// <summary>
        /// 停止所有线程
        /// </summary>
        public static void StopAll()
        {
            Log.Stop();

        }

        /// <summary>
        /// 获取指定时间的当周周一
        /// </summary>
        /// <param name="someDate">指定的时间</param>
        /// <returns>指定时间的当周周一的日期</returns>
        public static DateTime GetMondayDate(DateTime someDate)
        {
            int i = someDate.DayOfWeek - DayOfWeek.Monday;
            // i值 > = 0 ，因为枚举原因，Sunday排在最前，此时Sunday-Monday=-1，必须+7=6。 
            if (i == -1) i = 6;
            return someDate.AddDays(-i);
        }
        /// <summary>
        /// 获取指定时间的当周周日
        /// </summary>
        /// <param name="someDate">指定的时间</param>
        /// <returns>指定时间的当周周日的日期</returns>
        public static DateTime GetSundayDate(DateTime someDate)
        {
            int i = someDate.DayOfWeek - DayOfWeek.Sunday;
            if (i != 0) i = 7 - i;// 因为枚举原因，Sunday排在最前，相减间隔要被7减。
            return someDate.AddDays(i);
        }
        #endregion
    }
}
